import axios from 'axios';
import { message } from 'antd';
import * as type from './actionTypes'
import { hashHistory } from 'react-router';


// 获取建议列表
export const proposalPaging = (current, pageSize, _data) => {
    let param = {
        offset: (current-1)*pageSize,
        limit: pageSize,
        data: _data
    }
    let url = '/proposals';

    return (dispatch) => {
        axios.get(url, {params:param})
        .then(res => {
            // console.log(res)
            let obj = {
                loading: false,
                result: res.data
            }
            dispatch({
                type: type.GET_SUGGEST_DATA_SUCCESS,
                payload: obj
            })
        })
        .catch(err => {
            console.log(err)
            // message.error("接口异常！")
        })
    };

}

//根据id查询详情
export const queryProposalById = (id) => {
    let url = '/proposals/' + id;

    return (dispatch) => {
        axios.get(url)
        .then(res => {
            dispatch({
                type: type.GET_SUGGSET_DATA_BY_ID_SUCCESS,
                payload: {
                    loading: false,
                    result: res.data
                }
            })
        })
        .catch(err => {
            console.log(err)
            // message.error("接口异常！")
        })
    };
}

//删除一条记录
export const deleteProposalById = (id) => {
    let url = '/proposals/' + id;

    return (dispatch) => {
        axios.delete(url)
        .then(res => {
            // console.log(res)
            if(res.data.data){
                message.success("删除成功！")
                // this.forceUpdate(); 
            }else{
                message.error("删除失败！")
            }
            dispatch({
                type: type.DELETE_SUGGEST_BY_ID_SUCCESS,
                payload: {
                    loading: false,
                    id: id
                }
            })
        })
        .catch(err => {
            console.log(err)
            // message.error("接口异常！")
        })
    };

}

//新增一条记录
export const addProposal = (data) => {
    let url = '/proposals';

    return (dispatch) => {
        axios.post(url,data)
        .then(res => {
            // console.log(res)
            if(res.data.data){
                message.success("新增成功！")
                hashHistory.goBack()
            }else{
                message.error("新增失败！")
                hashHistory.goBack()
            }
            dispatch({
                type: type.ADD_SUGGSET_DATA_BY_ID_SUCCESS,
                payload: {
                    loading: false,
                    // result: res.data
                }
            })
        })
        .catch(err => {
            console.log(err)
            // message.error("接口异常！")
        })
    };
}

//修改一条记录
export const updateProposal = (id, data) => {
    let url = '/proposals/' + id;

    return (dispatch) => {
        axios.put(url,data)
        .then(res => {
            // console.log(res)
            dispatch({
                type: type.UPDATE_SUGGSET_DATA_BY_ID_SUCCESS,
                payload: {
                    loading: false,
                    result: res.data
                }
            })
        })
        .catch(err => {
            console.log(err)
            // message.error("接口异常！")
        })
    };
}